package unibo.lsb.test;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import unibo.lsb.communication.CommunicationRemote;
import unibo.lsb.communication.InformationSingleton;
import unibo.lsb.logic.Player;
import unibo.lsb.logic.PlayerId;
import unibo.lsb.logic.PlayerSelfSingleton;
import unibo.lsb.logic.PlayersTable;
import unibo.lsb.server.ConfigurationConstants;

public class pollingTest2 {

	public static void main(String[] args) {

		PlayerId A = new PlayerId("A", "", "127.0.0.1", 1098);
		PlayerId B = new PlayerId("B", "", "127.0.0.1", 1097);
		PlayerId C = new PlayerId("C", "", "127.0.0.1", 1096);
		PlayerId D = new PlayerId("D", "", "127.0.0.1", 1095);
		PlayerId E = new PlayerId("E", "", "127.0.0.1", 1094);

		List<PlayerId> l = new ArrayList<PlayerId>();
		l.add(A);
		l.add(B);

		PlayersTable pt = new PlayersTable(l);

		Thread t = new Thread() {
			@Override
			public void run() {
				// variables
				CommunicationRemote commRemote = null;
				List<PlayerId> PlayersList = InformationSingleton.getInstance()
						.getGame().getListOfPlayers();
				int myIndex = PlayerSelfSingleton.getInstance().getSelf()
						.getIndexInTable();
				PlayersTable pt = InformationSingleton.getInstance().getpt();

				// main cycle for ever and ever
				while (true) {
					Player nextPlayer = pt.getNextAlivePlayer(myIndex);
					PlayerId nextPlayerId = nextPlayer.getPlayerId();
					System.out
							.println("MyNextAlivePlayerIndex " + nextPlayerId);

					String objectURL = "rmi://" + nextPlayerId.getIpAddress()
							+ ":" + nextPlayerId.getPort() + "/"
							+ ConfigurationConstants.INFORMATIONSERVER_NAME;

					try {
						commRemote = (CommunicationRemote) Naming
								.lookup(objectURL);
						System.out.println(commRemote
								+ "                <- lookup");
					} catch (java.rmi.ConnectException e) {
						/*
						 * DEAD!
						 */
						System.out.println(objectURL + " not started jet");
						// e.printStackTrace();
					} catch (MalformedURLException e) {
						e.printStackTrace();
					} catch (RemoteException e) {
						e.printStackTrace();
					} catch (NotBoundException e) {
						e.printStackTrace();
					}

					boolean live = true;
					while (live == true) {
						try {
							sleep(200);
							live = commRemote.isAlive();
							if (live == true) {
								System.out.println(objectURL + " è vivo? "
										+ live);
							}
						} catch (java.rmi.ConnectException e) {
							/*
							 * DEAD!
							 */
							live = false;
							System.out.println(objectURL + " is dead");

							// set dead on playerstable
							nextPlayer.setStatus(1);

							int index = pt.getTurnForPlayer(nextPlayer);
							Player playerLoc = pt.getPlayer(index);

							// invoke communication function for say to others
							/*
							 * try { notifyStatus(nextPlayer, false); } catch
							 * (TokioException e1) { catch block
							 * e1.printStackTrace(); }
							 */
							return;
						} catch (RemoteException e) {
							e.printStackTrace();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		};
		t.start();

		return;

	}

}
